<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <h2>直接通过FormData</h2>
    <input type="file" accept="image/*">
    <img src="" alt="" />
    <button>上传</button>
    <script>
        document.querySelector('button').addEventListener('click', function () {
            // console.log(document.querySelector('input').files)
            const data = new FormData()
            const img = document.querySelector('input').files[0]
            data.append('avatar', img)
            // console.log(data.get('avatar'))
            axios({
                url: "http://ajax-api.itheima.net/api/file",
                method: 'post',
                data,
            }).then((res) => {
                console.log(res)
                document.querySelector('img').src = res.data.data.url
            })
        })

    </script>
</body>

</html>